package com.dragon.sdgp.util.regexpDecode.handler;

import com.dragon.sdgp.util.regexpDecode.SyntaxErrorException;

import java.util.List;

/**
 * @AUTHOR : Dragon_Yang
 * @File : SymbolHandler
 * @DESCRIPTION :
 * @DATE : 2023/3/2 17:02
 */
public class SymbolHandler extends BaseDecodeHandler{
    @Override
    public List<MetaData> handle(String exp) {
        StringBuilder res = new StringBuilder(exp);
        replaceAll(res, "*");
        replaceAll(res, "?");
        replaceAll(res, "+");
        replaceBrace(res);
        check(res);
        return doNext(res.toString());
    }

    private void check(StringBuilder res) {
        int offset = -1;
        while ((offset = res.indexOf("↑", offset + 1)) != -1){
            if(res.charAt(offset - 1) != ']'){
                if(res.charAt(offset - 2) == '\\'){
                    res.replace(offset - 2, offset - 1, "→←\\");
                }
            }
            offset += 2;
        }
    }

    private void replaceBrace(StringBuilder res){
        int offset = -1;
        while ((offset = res.indexOf("{", offset + 1)) != -1){
            res.insert(offset, "↑");
            offset++;
        }
        offset = -1;
        while ((offset = res.indexOf("}", offset + 1)) != -1){
            res.insert(offset + 1, "→←");
            offset += 2;
        }
        offset = -1;
        while ((offset = res.indexOf("[", offset + 1)) != -1){
            res.insert(offset, "→←");
            offset += 2;
        }
        offset = -1;
        while ((offset = res.indexOf("]", offset + 1)) != -1){
            if(res.charAt(offset + 1) != '↑'){
                res.insert(offset + 1, "→←");
                offset += 2;
            }
        }
    }

    private void replaceAll(StringBuilder res, String target) throws SyntaxErrorException {
        int offset = -1;
        while ((offset = res.indexOf(target, offset + 1)) != -1){
            try {
                if(res.charAt(offset - 1) != ']' && res.charAt(offset - 1) != ')'){
                    if(res.charAt(offset - 1) == '←') throw new SyntaxErrorException();
                    if(offset - 2 >= 0 && res.charAt(offset - 2) == '\\'){
                        res.insert(offset - 2, "→←");
                    }else {
                        res.insert(offset - 1, "→←");
                    }
                    offset += 2;
                }else if(res.charAt(offset - 1) == ')' || res.charAt(offset - 1) == ']'){
                    int arrowLeft = res.lastIndexOf("←", offset);
                    int left = res.lastIndexOf("(", offset);
                    if(left == -1) left = res.lastIndexOf("[", offset);
                    if(left == -1 || left <= arrowLeft) throw new SyntaxErrorException();
                    res.insert(left, "→←");
                    offset += 2;
                }
            }catch (StringIndexOutOfBoundsException e){
                throw new SyntaxErrorException();
            }

            //将符号与待匹配部分分割
            StringBuilder replaceStr = new StringBuilder("↑");
            switch (target){
                case "*": replaceStr.append("{0,10}");break;
                case "+": replaceStr.append("{1,10}");break;
                case "?": replaceStr.append("{0,1}");break;
            }
            replaceStr.append("→←");
            res.replace(offset, offset + 1, replaceStr.toString());
        }
    }
}
